import type { flowable } from '@/index.d';
import { api, urls } from '@/index';
import qs from 'qs';

//物理删除一个对象
export const physicalDeleteByIdInFlowable = async (url: string, id: string) => {
  try {
    const res = await api({ method: 'delete', url: url + '/physical/' + id });
    if (res.status === 200) {
      const resData = res.data;
      ElMessage({
        message: resData.msg,
        type: resData.type
      });
      return resData.status;
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const getProcessDefinitionVOByCondition = async (
  key: string,
  value: string
): Promise<flowable.ProcessDefinitionVO | undefined> => {
  try {
    const res = await api({
      method: 'get',
      url: `${urls.processDefinition}/getOneByCondition/${key}/${value}`
    });
    if (res.status === 200) {
      if (res.data.status) {
        return res.data.body as flowable.ProcessDefinitionVO;
      } else {
        ElMessage.error(res.data.msg);
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const getProcessDefinitionVOByConditions = async (
  queryConditions: flowable.ProcessDefinitionQueryCondition
): Promise<flowable.ProcessDefinitionVO | undefined> => {
  try {
    const res = await api({
      method: 'post',
      url: urls.processDefinition + '/getOneByConditions',
      data: qs.stringify(queryConditions)
    });
    if (res.status === 200) {
      if (res.data.status) {
        return res.data.body as flowable.ProcessDefinitionVO;
      } else {
        ElMessage.error(res.data.msg);
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const getProcessDefinitionVOListByCondition = async (
  key: string,
  value: string
): Promise<Array<flowable.ProcessDefinitionVO> | undefined> => {
  try {
    const res = await api({
      method: 'get',
      url: `${urls.processDefinition}/getListByCondition/${key}/${value}`
    });
    if (res.status === 200) {
      if (res.data.status) {
        return res.data.body as Array<flowable.ProcessDefinitionVO>;
      } else {
        ElMessage.error(res.data.msg);
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const getProcessDefinitionVOListByConditions = async (
  queryConditions: flowable.ProcessDefinitionQueryCondition
): Promise<Array<flowable.ProcessDefinitionVO> | undefined> => {
  try {
    const res = await api({
      method: 'post',
      url: urls.processDefinition + '/getListByConditions',
      data: qs.stringify(queryConditions)
    });
    if (res.status === 200) {
      if (res.data.status) {
        return res.data.body as Array<flowable.ProcessDefinitionVO>;
      } else {
        ElMessage.error(res.data.msg);
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const getProcessDefinitionVOListByConditionsWithPage = async (
  queryConditions: flowable.ProcessDefinitionQueryCondition
): Promise<flowable.FlowablePage<flowable.ProcessDefinitionVO> | undefined> => {
  try {
    const res = await api({
      method: 'post',
      url: urls.processDefinition + '/getListByConditionsWithPage',
      data: qs.stringify(queryConditions)
    });
    if (res.status === 200) {
      if (res.data.status) {
        return res.data.body as flowable.FlowablePage<flowable.ProcessDefinitionVO>;
      } else {
        ElMessage.error(res.data.msg);
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const getProcessInstanceVOByCondition = async (
  key: string,
  value: string
): Promise<flowable.ProcessInstanceVO | undefined> => {
  try {
    const res = await api({
      method: 'get',
      url: `${urls.processInstance}/getOneByCondition/${key}/${value}`
    });
    if (res.status === 200) {
      if (res.data.status) {
        return res.data.body as flowable.ProcessInstanceVO;
      } else {
        ElMessage.error(res.data.msg);
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const getProcessInstanceVOByConditions = async (
  queryConditions: flowable.ProcessInstanceQueryCondition
): Promise<flowable.ProcessInstanceVO | undefined> => {
  try {
    const res = await api({
      method: 'post',
      url: urls.processInstance + '/getOneByConditions',
      data: qs.stringify(queryConditions)
    });
    if (res.status === 200) {
      if (res.data.status) {
        return res.data.body as flowable.ProcessInstanceVO;
      } else {
        ElMessage.error(res.data.msg);
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const getProcessInstanceVOListByCondition = async (
  key: string,
  value: string
): Promise<Array<flowable.ProcessInstanceVO> | undefined> => {
  try {
    const res = await api({
      method: 'get',
      url: `${urls.processInstance}/getListByCondition/${key}/${value}`
    });
    if (res.status === 200) {
      if (res.data.status) {
        return res.data.body as Array<flowable.ProcessInstanceVO>;
      } else {
        ElMessage.error(res.data.msg);
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const getProcessInstanceVOListByConditions = async (
  queryConditions: flowable.ProcessInstanceQueryCondition
): Promise<Array<flowable.ProcessInstanceVO> | undefined> => {
  try {
    const res = await api({
      method: 'post',
      url: urls.processInstance + '/getListByConditions',
      data: qs.stringify(queryConditions)
    });
    if (res.status === 200) {
      if (res.data.status) {
        return res.data.body as Array<flowable.ProcessInstanceVO>;
      } else {
        ElMessage.error(res.data.msg);
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const getProcessInstanceVOListByConditionsWithPage = async (
  queryConditions: flowable.ProcessInstanceQueryCondition
): Promise<flowable.FlowablePage<flowable.ProcessInstanceVO> | undefined> => {
  try {
    const res = await api({
      method: 'post',
      url: urls.processInstance + '/getListByConditionsWithPage',
      data: qs.stringify(queryConditions)
    });
    if (res.status === 200) {
      if (res.data.status) {
        return res.data.body as flowable.FlowablePage<flowable.ProcessInstanceVO>;
      } else {
        ElMessage.error(res.data.msg);
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const getCommentListByProcessInstanceId = async (
  processInstanceId: string
): Promise<Array<flowable.TaskInstanceVO> | undefined> => {
  try {
    const res = await api({
      method: 'get',
      url: `${urls.taskInstance}/getCommentListByProcessInstanceId/${processInstanceId}`
    });
    if (res.status === 200) {
      if (res.data.status) {
        return res.data.body as Array<flowable.TaskInstanceVO>;
      } else {
        ElMessage.error(res.data.msg);
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const getTaskInstanceVOListByCondition = async (
  key: string,
  value: string
): Promise<Array<flowable.TaskInstanceVO> | undefined> => {
  try {
    const res = await api({
      method: 'get',
      url: `${urls.taskInstance}/getListByCondition/${key}/${value}`
    });
    if (res.status === 200) {
      if (res.data.status) {
        return res.data.body as Array<flowable.TaskInstanceVO>;
      } else {
        ElMessage.error(res.data.msg);
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};
